package service

import (
	"blizzard/app/dao"
	"blizzard/app/model"
	"blizzard/app/utils/tool"
	"github.com/gogf/gf/crypto/gmd5"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/net/ghttp"
)

var Admin = new(serviceAdmin)

type serviceAdmin struct {}

func (s *serviceAdmin) Login(r *ghttp.Request, m *model.AdminLoginParam) error {
	one, err := dao.BxMenber.FindOne("username = ?", m.Username)
	if err != nil {
		return err
	}
	if one.Password != gmd5.MustEncryptString(m.Password + one.Salt) {
		return gerror.New("密码错误")
	}
	ti := tool.GetTimeString()
	ip := r.GetClientIp()
	err = dao.BxMenber.UPAdmin(m.Username, ip, ti)
	if err != nil {
		return err
	}
	return nil
}

func (s *serviceAdmin) Logout(r *ghttp.Request, m *model.AdminLoginParam) error {
	one, err := dao.BxMenber.FindOne("username = ?", m.Username)
	if err != nil {
		return err
	}
	if one.Password != gmd5.MustEncryptString(m.Password + one.Salt) {
		return gerror.New("密码错误")
	}
	return nil
}